<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    // 方法1: Set
    function uniqueArraySet(arr) {
      return [...new Set(arr)]
    }
    console.log("Set去重:", uniqueArraySet([1, 2, 2, 3, 4, 4, 5])) // [1, 2, 3, 4, 5]

    // 方法2: filter + indexOf
    function uniqueArrayFilter(arr) {
      return arr.filter((item, index) => arr.indexOf(item) === index)
    }
    console.log("filter去重:", uniqueArrayFilter([1, 2, 2, 3, 4, 4, 5])) // [1, 2, 3, 4, 5]

    // 方法2: filter + indexOf
    function uniqueArrayFilter1(arr) {
      return [...new Set(arr)]
    }
    console.log("filter去重:", uniqueArrayFilter1([1, 2, 2, 3, 4, 4, 5]), 2323) // [1, 2, 3, 4, 5]

    // 方法3: 排序后比较相邻元素
    function uniqueArraySort(arr) {
      if (arr.length < 2) return arr
      arr.sort((a, b) => a - b)
      const result = [arr[0]]
      for (let i = 1; i < arr.length; i++) {
        if (arr[i] !== arr[i - 1]) {
          result.push(arr[i])
        }
      }
      return result
    }
    console.log("排序后去重:", uniqueArraySort([1, 2, 2, 3, 4, 4, 5, 1])) // [1, 2, 3, 4, 5]
  </script>
</html>
